package com.flannery.learningandroid.learnview

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.flannery.learningandroid.R
import kotlinx.android.synthetic.main.learning_recyclerview.*
import kotlin.random.Random

class RecyclerViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.learning_recyclerview)

        btnToast.setOnClickListener {
            Toast.makeText(this, "Toast", Toast.LENGTH_SHORT).show()
        }

        btnListView.setOnClickListener {
            initList()
        }

        btngridView.setOnClickListener {
            initGrid()
        }

        btnstagger.setOnClickListener {
            initStagger()
        }


    }

    //====listview=======
    fun initList() {
        var list = arrayListOf(
            "赵",
            "钱",
            "孙",
            "李",
            "周",
            "吴",
            "郑",
            "王",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "赵",
            "钱",
            "孙",
            "李",
            "周",
            "吴",
            "郑",
            "王",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8"
        )

        mRecyclerView.adapter = ListAdapter(list)
        mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }

    inner class ListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title = itemView.findViewById<TextView>(R.id.title)
    }

    inner class ListAdapter(var list: List<String>) : RecyclerView.Adapter<ListHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
            var itemView = LayoutInflater.from(parent.context)
                .inflate(R.layout.learning_recyclerview_item_list, parent, false)
            return ListHolder(itemView)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: ListHolder, position: Int) {
            holder.title.text = list.get(position)
        }

    }


    //====gridview=======
    fun initGrid() {
        var list = arrayListOf(
            "赵",
            "钱",
            "孙",
            "李",
            "周",
            "吴",
            "郑",
            "王",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8"
        )
        mRecyclerView.adapter = ListAdapter(list)
        mRecyclerView.layoutManager = GridLayoutManager(this, 2)
    }

    //====staggerview=======
    fun initStagger() {
        var list = arrayListOf(
            "赵",
            "钱",
            "孙",
            "李",
            "周",
            "吴",
            "郑",
            "王",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "赵",
            "钱",
            "孙",
            "李",
            "周",
            "吴",
            "郑",
            "王",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8"
        )
        mRecyclerView.adapter = StagerAdapter(list)
        mRecyclerView.layoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
    }

    open inner class StagerAdapter(var list: List<String>) : RecyclerView.Adapter<ListHolder>() {

        var random = Random(1)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
            var itemView = LayoutInflater.from(parent.context)
                .inflate(R.layout.learning_recyclerview_item_list, parent, false)
            return ListHolder(itemView)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: ListHolder, position: Int) {
            holder.title.text = list.get(position)
            holder.title.setTextColor(Color.BLACK)


            //var random = Random(100)
            var nextFloat = random.nextFloat() * 100
            holder.title.textSize = nextFloat
        }

    }
}
